home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / models / RTMPModel.as < prev    next >
Text File  |  2008-11-04  |  11KB  |  322 lines

  1. package com.jeroenwijering.models
  2. {
  3.    import com.jeroenwijering.events.ModelEvent;
  4.    import com.jeroenwijering.events.ModelStates;
  5.    import com.jeroenwijering.player.Model;
  6.    import com.jeroenwijering.utils.NetClient;
  7.    import com.meychi.ascript.TEA;
  8.    import flash.events.AsyncErrorEvent;
  9.    import flash.events.ErrorEvent;
  10.    import flash.events.IOErrorEvent;
  11.    import flash.events.NetStatusEvent;
  12.    import flash.events.SecurityErrorEvent;
  13.    import flash.media.SoundTransform;
  14.    import flash.media.Video;
  15.    import flash.net.NetConnection;
  16.    import flash.net.NetStream;
  17.    import flash.net.ObjectEncoding;
  18.    import flash.net.Responder;
  19.    import flash.utils.clearInterval;
  20.    import flash.utils.clearTimeout;
  21.    import flash.utils.setInterval;
  22.    
  23.    public class RTMPModel implements ModelInterface
  24.    {
  25.        
  26.       
  27.       private var stream:NetStream;
  28.       
  29.       private var timeinterval:Number;
  30.       
  31.       private var timeout:Number;
  32.       
  33.       private var connection:NetConnection;
  34.       
  35.       private var model:Model;
  36.       
  37.       private var transform:SoundTransform;
  38.       
  39.       private var metadata:Boolean;
  40.       
  41.       private var video:Video;
  42.       
  43.       public function RTMPModel(param1:Model)
  44.       {
  45.          super();
  46.          model = param1;
  47.          connection = new NetConnection();
  48.          connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
  49.          connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  50.          connection.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  51.          connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler);
  52.          connection.objectEncoding = ObjectEncoding.AMF0;
  53.          connection.client = new NetClient(this);
  54.          video = new Video(320,240);
  55.          quality(model.config["quality"]);
  56.          transform = new SoundTransform();
  57.          if(model.config["mute"] == true)
  58.          {
  59.             volume(0);
  60.          }
  61.          else
  62.          {
  63.             volume(model.config["volume"]);
  64.          }
  65.       }
  66.       
  67.       private function streamlengthHandler(param1:Number) : void
  68.       {
  69.          onData({
  70.             "type":"streamlength",
  71.             "duration":param1
  72.          });
  73.       }
  74.       
  75.       private function metaHandler(param1:ErrorEvent) : void
  76.       {
  77.          model.sendEvent(ModelEvent.META,{"error":param1.text});
  78.       }
  79.       
  80.       public function setStream() : void
  81.       {
  82.          var _loc1_:* = undefined;
  83.          var _loc2_:Responder = null;
  84.          var _loc3_:Responder = null;
  85.          var _loc4_:Responder = null;
  86.          _loc1_ = getID(model.playlist[model.config["item"]]["file"]);
  87.          stream = new NetStream(connection);
  88.          stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
  89.          stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  90.          stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler);
  91.          stream.bufferTime = model.config["bufferlength"];
  92.          stream.client = new NetClient(this);
  93.          video.attachNetStream(stream);
  94.          stream.soundTransform = transform;
  95.          stream.play(_loc1_);
  96.          _loc2_ = new Responder(streamlengthHandler);
  97.          _loc3_ = new Responder(referrerHandler);
  98.          _loc4_ = new Responder(pageURLHandler);
  99.          connection.call("getStreamLength",_loc2_,_loc1_);
  100.          connection.call("getReferrer",_loc3_,_loc1_);
  101.          connection.call("getPageUrl",_loc4_,_loc1_);
  102.          connection.call("checkBandwidth",null);
  103.          clearInterval(timeinterval);
  104.          timeinterval = setInterval(timeHandler,100);
  105.       }
  106.       
  107.       private function pageURLHandler(param1:String) : void
  108.       {
  109.          onData({
  110.             "type":"pageurl",
  111.             "url":param1
  112.          });
  113.       }
  114.       
  115.       private function timeHandler() : void
  116.       {
  117.          var _loc1_:* = undefined;
  118.          var _loc2_:* = undefined;
  119.          var _loc3_:* = undefined;
  120.          _loc1_ = Math.round(stream.bufferLength / stream.bufferTime * 100);
  121.          _loc2_ = Math.round(stream.time * 10) / 10;
  122.          _loc3_ = model.playlist[model.config["item"]]["duration"];
  123.          if(_loc1_ < 100 && _loc2_ < Math.abs(_loc3_ - stream.bufferTime - 1))
  124.          {
  125.             model.sendEvent(ModelEvent.BUFFER,{"percentage":_loc1_});
  126.             if(model.config["state"] != ModelStates.BUFFERING)
  127.             {
  128.                connection.call("checkBandwidth",null);
  129.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  130.             }
  131.          }
  132.          else if(model.config["state"] == ModelStates.BUFFERING)
  133.          {
  134.             model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  135.          }
  136.          if(_loc3_ > 0)
  137.          {
  138.             model.sendEvent(ModelEvent.TIME,{
  139.                "position":_loc2_,
  140.                "duration":_loc3_
  141.             });
  142.          }
  143.       }
  144.       
  145.       public function stop() : void
  146.       {
  147.          clearInterval(timeinterval);
  148.          connection.close();
  149.          if(stream)
  150.          {
  151.             stream.close();
  152.          }
  153.          video.attachNetStream(null);
  154.       }
  155.       
  156.       private function statusHandler(param1:NetStatusEvent) : void
  157.       {
  158.          if(param1.info.code == "NetConnection.Connect.Success")
  159.          {
  160.             if(param1.info.secureToken != undefined)
  161.             {
  162.                connection.call("secureTokenResponse",null,TEA.decrypt(param1.info.secureToken,model.config["token"]));
  163.             }
  164.             if(model.config["subscribe"])
  165.             {
  166.                timeout = setInterval(subscribe,2000,model.playlist[model.config["item"]]["file"]);
  167.             }
  168.             else
  169.             {
  170.                setStream();
  171.             }
  172.          }
  173.          else if(param1.info.code == "NetStream.Seek.Notify")
  174.          {
  175.             clearInterval(timeinterval);
  176.             timeinterval = setInterval(timeHandler,100);
  177.          }
  178.          else if(param1.info.code == "NetStream.Play.StreamNotFound" || param1.info.code == "NetConnection.Connect.Rejected" || param1.info.code == "NetConnection.Connect.Failed")
  179.          {
  180.             stop();
  181.             model.sendEvent(ModelEvent.ERROR,{"message":"Stream not found: " + model.playlist[model.config["item"]]["file"]});
  182.          }
  183.          else
  184.          {
  185.             model.sendEvent(ModelEvent.META,{"info":param1.info.code});
  186.          }
  187.       }
  188.       
  189.       private function errorHandler(param1:ErrorEvent) : void
  190.       {
  191.          model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
  192.       }
  193.       
  194.       public function load() : void
  195.       {
  196.          model.mediaHandler(video);
  197.          connection.connect(model.config["streamer"]);
  198.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  199.       }
  200.       
  201.       private function referrerHandler(param1:String) : void
  202.       {
  203.          onData({
  204.             "type":"referrer",
  205.             "url":param1
  206.          });
  207.       }
  208.       
  209.       public function onData(param1:Object) : void
  210.       {
  211.          if(param1.type == "metadata" && !metadata)
  212.          {
  213.             if(param1.width)
  214.             {
  215.                video.width = param1.width;
  216.                video.height = param1.height;
  217.             }
  218.             if(model.playlist[model.config["item"]]["start"] > 0)
  219.             {
  220.                seek(model.playlist[model.config["item"]]["start"]);
  221.             }
  222.             metadata = true;
  223.          }
  224.          else if(param1.type == "complete")
  225.          {
  226.             clearInterval(timeinterval);
  227.             model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
  228.          }
  229.          else if(param1.type == "fcsubscribe")
  230.          {
  231.             if(param1.code == "NetStream.Play.StreamNotFound")
  232.             {
  233.                stop();
  234.                model.sendEvent(ModelEvent.ERROR,{"message":"Subscription failed: " + model.playlist[model.config["item"]]["file"]});
  235.             }
  236.             else if(param1.code == "NetStream.Play.Start")
  237.             {
  238.                setStream();
  239.             }
  240.             clearInterval(timeout);
  241.          }
  242.          model.sendEvent(ModelEvent.META,param1);
  243.       }
  244.       
  245.       private function getID(param1:String) : String
  246.       {
  247.          if(param1.substr(-4) == ".mp3")
  248.          {
  249.             param1 = "mp3:" + param1.substr(0,param1.length - 4);
  250.          }
  251.          else if(param1.substr(-4) == ".mp4" || param1.substr(-4) == ".mov" || param1.substr(-4) == ".aac" || param1.substr(-4) == ".m4a")
  252.          {
  253.             param1 = "mp4:" + param1.substr(0,param1.length - 4);
  254.          }
  255.          else if(param1.substr(-4) == ".flv")
  256.          {
  257.             param1 = param1.substr(0,param1.length - 4);
  258.          }
  259.          return param1;
  260.       }
  261.       
  262.       public function volume(param1:Number) : void
  263.       {
  264.          transform.volume = param1 / 100;
  265.          if(stream)
  266.          {
  267.             stream.soundTransform = transform;
  268.          }
  269.       }
  270.       
  271.       public function play() : void
  272.       {
  273.          clearTimeout(timeout);
  274.          clearInterval(timeinterval);
  275.          stream.resume();
  276.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  277.          timeinterval = setInterval(timeHandler,100);
  278.       }
  279.       
  280.       private function subscribe(param1:String) : void
  281.       {
  282.          connection.call("FCSubscribe",null,param1);
  283.       }
  284.       
  285.       public function seek(param1:Number) : void
  286.       {
  287.          clearTimeout(timeout);
  288.          clearInterval(timeinterval);
  289.          if(model.config["state"] == ModelStates.PAUSED)
  290.          {
  291.             stream.resume();
  292.          }
  293.          else
  294.          {
  295.             model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  296.          }
  297.          stream.seek(param1);
  298.       }
  299.       
  300.       public function pause() : void
  301.       {
  302.          clearInterval(timeinterval);
  303.          stream.pause();
  304.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
  305.       }
  306.       
  307.       public function quality(param1:Boolean) : void
  308.       {
  309.          if(param1 == true)
  310.          {
  311.             video.smoothing = true;
  312.             video.deblocking = 3;
  313.          }
  314.          else
  315.          {
  316.             video.smoothing = false;
  317.             video.deblocking = 1;
  318.          }
  319.       }
  320.    }
  321. }
  322.